"""
Case Type   : JDBC+mysql和opengauss兼容B库对比
Case Name   : JDBC默认参数+tinyint类型不带zerofill属性，隐式/显式类型转换
Create At   : 2023/10/16
Owner       : ningyali
Description :
    1.读取通用tinyint SQL脚本
    2.复制jdbc到当前目录
    3.生成java文件,通过JDBC连接数据库,执行sql
    4.编译上一步生成的java文件，生成可执行文件
    5.执行上一步生成的可执行文件，对mysql和opengauss兼容B库结果进行对比
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功
    5.执行成功，结果一致
History     :
"""
import os
import unittest

from yat.test import Node

from testcase.CONNECTORS.JDBC.CompatibilityB.DATATYPE. \
    Opengauss_Function_Connect_JDBC_Common import ComJDBC
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class MysqlTest3(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.comjdbc = ComJDBC()
        self.Constant = Constant()
        self.mysql_node = Node('mysql5')
        self.og_node = Node('PrimaryDbBmode')
        self.mysql_info = {'f_name': 'mysql_jdbc_datatype3',
                           'db_type': 'mysql',
                           'db_host': self.mysql_node.db_host,
                           'db_port': self.mysql_node.db_port,
                           'db_name': self.mysql_node.db_name,
                           'db_user': self.mysql_node.db_user,
                           'db_passwd': self.mysql_node.db_password,
                           'db_driver': 'com.mysql.jdbc.Driver',
                           'sql': ''}
        self.og_info = {'f_name': 'opengauss_jdbc_datatype3',
                        'db_type': 'opengauss',
                        'db_host': self.og_node.db_host,
                        'db_port': self.og_node.db_port,
                        'db_name': self.og_node.db_name,
                        'db_user': self.og_node.db_user,
                        'db_passwd': self.og_node.db_password,
                        'db_driver': 'org.opengauss.Driver',
                        'sql': ''}
        self.sql_path = os.path.join(
            'testcase', 'script', 'CompatibilityB', 'DATATYPE', 'TINYINT',
            'CompatibilityB_DataType_Tinyint_Case0003.sql')

    def test_1(self):
        text = 'step1:读取tinyint SQL脚本; expect:执行成功'
        self.log.info(f'-----{text}-----')
        self.log.info(self.sql_path)
        sql = self.comjdbc.read_sql_file(self.sql_path)
        self.assertNotEqual(len(sql.strip()), 0, f'执行失败：{text}')
        og_params = [['add', 22, 'set dolphin.b_compatibility_mode=on;'],
                     ['add', -1, 'set dolphin.b_compatibility_mode=off;'],
                     ['add', 23, 'set bytea_output=escape;'],
                     ['add', -1, 'set bytea_output=hex;']]
        og_sql = self.comjdbc.characteristic_sql(sql, *og_params)
        self.mysql_info['sql'] = sql
        self.og_info['sql'] = og_sql

        text = 'step2:复制jdbc到当前目录; expect:执行成功'
        self.log.info(f'-----{text}-----')
        res = self.comjdbc.get_jdbc(self.mysql_info['db_type'])
        self.assertEqual(len(res.strip()), 0, f'mysql执行失败：{text}')
        res = self.comjdbc.get_jdbc(self.og_info['db_type'])
        self.assertEqual(len(res.strip()), 0, f'opengauss执行失败：{text}')

        text = 'step3:生成java文件,通过JDBC连接数据库,执行sql; expect:执行成功'
        self.log.info(f'-----{text}-----')
        self.comjdbc.write_java_file(**self.mysql_info)
        self.comjdbc.write_java_file(**self.og_info)

        text = 'step4:编译上一步生成的java文件，生成可执行文件; expect:执行成功'
        self.log.info(f'-----{text}-----')
        res = self.comjdbc.compile_java_file(self.mysql_info['db_type'])
        self.assertEqual(len(res.strip()), 0, f'mysql执行失败：{text}')
        res = self.comjdbc.compile_java_file(self.og_info['db_type'])
        self.assertEqual(len(res.strip()), 0, f'opengauss执行失败：{text}')

        text = 'step5:执行上一步生成的可执行文件，对mysql和opengauss兼容B库结果进行对比; ' \
               'expect:执行成功，结果一致'
        self.log.info(f'-----{text}-----')
        self.log.info('-----配置参数以使兼容B库可使用set配置参数值-----')
        guc_res = self.comjdbc.pri_sh.execute_gsguc(
            'reload',
            self.Constant.GSGUC_SUCCESS_MSG,
            'b_format_behavior_compat_options=\'enable_set_variables\'')
        self.assertTrue(guc_res, f'opengauss执行失败：{text}\nguc配置参数配置失败')
        mysql_res, _, _ = self.comjdbc.run_java_file(
            self.mysql_info['db_type'])
        og_res, _, _ = self.comjdbc.run_java_file(self.og_info['db_type'])

        self.log.info('-----对比mysql和opengauss兼容B库执行结果-----')
        diff = self.comjdbc.compare_sql_err_res(mysql_res, og_res, 'mysql',
                                                'openGauss')
        self.assertEqual(len(diff), 0, '对比不一致')

    def tearDown(self):
        self.log.info('----run teardown----')
        self.log.info('-----清理环境-----')
        self.comjdbc.remove_java_file()
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
